<script>
import { mapState } from 'vuex'
import { allowableLoadList, allowableVolumeList } from '@/constants/vehicle/carModels'
import CarModelsForm from '@/views/vehicle/carModels/components/carModelsForm.vue'

export default {
  name: 'carModels',
  components: { CarModelsForm },
  data() {
    return {
      /* 获取车辆类型分页参数 */
      pageParams: {
        page: 1,
        pageSize: 10,
        allowableLoad: null,
        allowableVolume: null,
        id: null,
        name: null
      },
      deleteIds: [],
      loading: false,
      edit: false,
      dialogVisible: false,
      allowableLoadList,
      allowableVolumeList
    }
  },
  methods: {
    /*
    *获取车辆类型分页数据的回调函数
    *  */
    async fetchCarModelsList() {
      try {
        this.loading = true
        await this.$store.dispatch('carModelsStore/fetchCarModelsListAction', this.pageParams)
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },
    /*
    *pageSize发生变化的回调函数
    *  */
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchEmployeeList()
    },
    /*
    *page发生变化的回调函数
    *  */
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchCarModelsList()
    },

    /*
    *点击搜索按钮的回调函数
    *
    *  */
    handleSearch() {
      this.pageParams.page = 1
      this.fetchCarModelsList()
    },
    /*
    *点击重置按钮的回调函数
    *
    *  */
    handleReset() {
      this.pageParams.page = 1
      this.pageParams.pageSize = 10
      this.pageParams.id = null
      this.pageParams.allowableLoad = null
      this.pageParams.allowableVolume = null
      this.pageParams.name = null
      this.fetchCarModelsList()
    },
    /*
    *
    * 点击删除按钮的回调函数
    *  */
    async handleDelete(row) {
      try {
        const confirm = await this.$confirm('此操作将永久删除该车型, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(error => error)
        if (confirm === 'cancel') return
        const deleteResult = await this.$store.dispatch('carModelsStore/deleteCarModelsAction', row.id)
        await this.fetchCarModelsList()
        this.$message.success(deleteResult)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /*
    * 表格组件的勾选回调函数
    *  */
    handleSelectionChange(selectionList) {
      this.deleteIds = selectionList.map(item => item.id)
    },
    /*
    *
    * 批量删除车型的回调函数
    *  */
    async handleDeleteAll() {
      if (!this.deleteIds.length) return this.$message.warning('请勾选需要删除的车型')
      try {
        const promiseAll = await this.$store.dispatch('carModelsStore/handleDeleteAllAction', this.deleteIds)
        await this.fetchCarModelsList()
        this.$message.success(promiseAll[0])
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /*
    * 新增车型的回调函数
    *  */
    handleAdd() {
      this.edit = false
      this.dialogVisible = true
    },
    /*
    * 新增车型的回调函数
    *  */
    handleEdit(row) {
      this.edit = true
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.modelsForm.fetchCarModels(row.id)
      })
    },

    /* 点击确定按钮的回调函数 */
    async handleSubmit() {
      let message = null
      try {
        if (this.edit) {
          message = await this.$store.dispatch('carModelsStore/updateCarModelsAction', this.$refs.modelsForm.formData)
        } else {
          message = await this.$store.dispatch('carModelsStore/addCarModelsAction', this.$refs.modelsForm.formData)
        }
        this.dialogVisible = false
        await this.fetchCarModelsList()
        this.$message.success(message)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /*
    * 对话框组件关闭的回调
    * */
    closeDialog() {
      this.$refs.modelsForm.resetFormData()
    }
  },
  computed: {
    ...mapState('carModelsStore', ['carModelsList', 'counts']),
    title() {
      return this.edit ? '编辑车型' : '新增车型'
    }
  },
  mounted() {
    this.fetchCarModelsList()
  }
}
</script>

<template>
  <div class="car-models">
    <el-card shadow="never" class="search">
      <el-form inline label-width="100px" :model="pageParams">
        <el-form-item label="车型编号:">
          <el-input placeholder="请输入车型编号" v-model="pageParams.id"></el-input>
        </el-form-item>
        <el-form-item label="应载重量:">
          <el-select v-model="pageParams.allowableLoad">
            <el-option v-for="(item) in allowableLoadList" :value="item.value" :label="item.name" :key="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应载体积:">
          <el-select v-model="pageParams.allowableVolume">
            <el-option v-for="(item) in allowableVolumeList" :value="item.value" :label="item.name" :key="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆类型:">
          <el-input placeholder="请输入车辆类型" v-model="pageParams.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="never" class="body" v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255,255,255)">
      <div class="header">
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增车型</el-button>
        <el-button type="primary" icon="el-icon-delete" @click="handleDeleteAll">批量删除车型</el-button>

      </div>
      <el-table border stripe :data="carModelsList" :header-cell-style="{background:'#f4f6f8',color:'#969393'}" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50px" align="center"></el-table-column>
        <el-table-column type="index" label="序号" width="50px" align="center"></el-table-column>
        <el-table-column prop="id" label="车辆编号" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="name" label="车辆类型" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="num" label="车型数量" align="center"></el-table-column>
        <el-table-column prop="allowableLoad" label="应载重量(吨)" align="center"></el-table-column>
        <el-table-column prop="allowableVolume" label="应载体积(立方米)" align="center" width="130px"></el-table-column>
        <el-table-column prop="measureLong" label="长(米)" align="center"></el-table-column>
        <el-table-column prop="measureWidth" label="宽(米)" align="center"></el-table-column>
        <el-table-column prop="measureHigh" label="高(米)" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="text" @click="handleEdit(row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.page" :page-sizes="[2, 3, 4, 10]" :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="counts" background></el-pagination>

    </el-card>
    <el-dialog :title="title" :visible.sync="dialogVisible" @close="closeDialog" :close-on-click-modal="false">
      <car-models-form ref="modelsForm"></car-models-form>
      <template #footer>
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.car-models {
  padding: 20px;

  .search {
    .el-form-item {
      width: 380px;

      .el-select {
        width: 130%;
      }

      .el-input {
        width: 130%;
      }
    }
  }

  .body {
    margin-top: 20px;

    .el-table {
      margin: 10px 0;
    }

    .el-pagination {
      display: flex;
      justify-content: center;
    }
  }
}
</style>
